package suanfa;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Solution {
    public int myAtoi(String str) {
        int result = 0;
        if(str == null) {
            return result;
        }
        Pattern pattern = Pattern.compile("^[\\+\\-0-9]?[0-9]+");
        Matcher m = pattern.matcher(str.trim());
        if(m.find()){
            try {
                result = Integer.parseInt(m.group(0));
            } catch (NumberFormatException e) {
                if(m.group(0).startsWith("-")){
                    result = Integer.MIN_VALUE;
                } else {
                    result = Integer.MAX_VALUE;
                }
            }
        }
        return  result;

    }
}
